<?php
require_once 'Mobitv/Admin/Action.php';
class DeclineAction extends Mobitv_Admin_Action
{
    public function execute()
    {
        $this->checkPermission();
        $videoId = isset($_GET['id']) ? intval($_GET['id']) : 0;
        $videoDao = Vega_Dao::factory('Mobitv_Admin_Video');
        $video = $videoDao->getPendingVideoDetail($videoId);
        if (!$videoId || !$video) $this->forward404();

        if(!$videoDao->canApproveVideo($videoId)) {
            $this->gotoUrl('/video?type=pending');
        }

        if ($_POST) {
            $reason = $_POST['reason'];
            if ($videoDao->declineVideo($reason, $videoId)) {
                $this->updateLogAdmin($_SESSION['userid'], 'video', $videoId, 'disapprove');
                $idNextPendingVideo = $videoDao->getIdNextPendingVideo();
                if (!$idNextPendingVideo) {
                    $this->gotoUrl('/video?type=deleted', 'Đã từ chối kích hoạt video');
                } else {
                    $this->gotoUrl('/video/approve?id=' . $idNextPendingVideo, 'Đã từ chối kích hoạt video, mời duyệt video tiếp dưới đây...');
                }
            } else {
                $this->gotoUrl('/video?type=deleted', 'Lỗi hệ thống');
            }
        }
        $this->_view->assign('video', $video);
        
        $ignore_reasons = $videoDao->getListIgnoreReason();
        $this->_view->assign('ignore_reasons', $ignore_reasons);
    }
}